<?php

namespace app\admin\controller;

use app\admin\controller\common\Admin;
use app\admin\controller\common\Punav;
use app\admin\controller\common\Yan_zheng;
use think\Db;
use think\Request;
use think\Validate;

class Count extends Admin
{

    //删除进度
    public function del_speed ()
    {
        $Yan_zheng = new Yan_zheng();
        if(($error = $Yan_zheng ->is_number([$_POST['id']])) != 1){
            return $error;
        }
        $state = Db::table('q_speed')->delete($_POST['id']);
        $Yan_zheng ->rizhi("删除一个进度,状态：{$state},id:{$_POST['id']}");
        return $state;
    }
    //删除手续支付
    public function del_money()
    {
        $Yan_zheng = new Yan_zheng();
        if(($error = $Yan_zheng ->is_number([$_POST['id']])) != 1){
            return $error;
        }
        $state = Db::table('q_appropriation')->delete($_POST['id']);
        $Yan_zheng ->rizhi("删除一个手续支付,状态：{$state},id:{$_POST['id']}");
        return $state;
    }
    //获取数据统计 中标价格
    public function count_sum()
    {
        $wh['data.id']=['>',0];
        if(isset($_GET['date']) && !empty($_GET['date'])){
            $wh['data.date']=['=',$_GET['date']];
        }
        if(isset($_GET['school']) && !empty($_GET['school'])){
            $wh['data.school']=['like',"%{$_GET['school']}%"];
        }
        if(isset($_GET['name']) && !empty($_GET['name'])){
            $wh['data.name']=['like',"%{$_GET['name']}%"];
        }
        //条件id
        if(isset($_GET['id']) && !empty($_GET['id'])){
            $wh['data.school']=['=',$_GET['id']];
        }
        //拨出金额
        $money = Db::table('q_datalist')
            ->alias('data')
            ->where($wh)
            ->join('q_money mo','data.id = mo.panen_id','left')
            ->field("
            sum(mo.money) as money
            ")
            ->select();
            //决算 中标金额
        $zhong_biao = Db::table('q_datalist')
            ->alias('data')
            ->where($wh)
            ->sum('budgetary_price');
        $jue_suan= Db::table('q_datalist')
            ->alias('data')
            ->where($wh)
            ->sum('determine_mobile');
        $json = [
            ['name'=>'中标金额','money'=>$zhong_biao],
            ['name'=>'决算金额','money'=>$jue_suan],
            ['name'=>'拨付金额','money'=>$money[0]['money']],
            ['name'=>'欠拨金额','money'=>$jue_suan-$money[0]['money']],
        ];
        return $json;
    }
    //获取数据统计 手续支付价
    public function procedures_sum()
    {
        $wh['data.id']=['>',0];
        if(isset($_GET['date']) && !empty($_GET['date'])){
            $wh['data.date']=['=',$_GET['date']];
        }
        if(isset($_GET['school']) && !empty($_GET['school'])){
            $wh['data.school']=['like',"%{$_GET['school']}%"];
        }
        if(isset($_GET['name']) && !empty($_GET['name'])){
            $wh['data.name']=['like',"%{$_GET['name']}%"];
        }
        //条件id
        if(isset($_GET['id']) && !empty($_GET['id'])){
            $wh['data.school']=['=',$_GET['id']];
        }
        //拨出金额
        $money = Db::table('q_datalist')
            ->alias('data')
            ->where($wh)
            ->join('q_start sta','data.id = sta.parend_id','left')
            ->join('q_appropriation app','sta.id = app.parent_id','left')
            ->field("
            sum(app.money) as payment
            ")
            ->select();
        $hejifeiyong = Db::table('q_datalist')
            ->alias('data')
            ->where($wh)
            ->join('q_start sta','data.id = sta.parend_id','left')
            ->field("
            sum(sta.money) as payment
            ")
            ->select();
        $json = [
            ['name'=>'合计费用','money'=>$hejifeiyong[0]['payment']],
            ['name'=>'已付费用','money'=>$money[0]['payment']],
            ['name'=>'未付费用','money'=>$hejifeiyong[0]['payment']-$money[0]['payment']]
        ];
        return $json;
    }
    //支付详细
    public function loock($id)
    {
        if(\request() ->isAjax()){
            return Db::table('q_appropriation')->where('parent_id',$id)->select();
        }
        return view();
    }
    //添加手续支付
    public function money_info()
    {
        if(\request()->isAjax()){
            $da['parent_id'] = input('post.parend_id');
            $da['money'] = input('post.money');
            $da['img'] = input('post.money_img');
            $Yan_zheng = new Yan_zheng();
            if(($error = $Yan_zheng ->is_string($da)) != 1){
                return $error;
            }
            $state =  Db::table('q_appropriation')->insert($da);
            $Yan_zheng ->rizhi("添加一个手续支付，状态：{$state},内容：".implode('=',$da));
            return $state;
        }
        return view();
    }
    //计算拨出的钱
    public function over_sum()
    {

        return Db::table('q_money')
            ->where('panen_id',input('post.id'))
            ->where('state',1)
            ->sum('money');
    }
    //修改拨出资金
    public function up_info($id)
    {
        $Yan_zheng = new Yan_zheng();
        if(\request() ->isAjax()){

            $da['id'] = input('post.id');
            $da['money'] = input('post.money');
            $da['money_img'] = input('post.money_img');
            $da['state'] = 0;
            if(($error = $Yan_zheng ->is_string($da)) != 1){
                return $error;
            }
            $state = Db::table('q_money')->update($da);
            $Yan_zheng ->rizhi("修改拨出金额，状态：{$state},内容是：".implode('=',$da));
            return $state;
        }
        if(($error = $Yan_zheng ->is_number([$id])) != 1){
            return $error;
        }

        $vi['data'] = Db::table('q_money')->find($id);
        return view('',$vi);
    }
    //通过拒绝拨出金额
    public function money_update()
    {
        $da['id']=input('post.id');
        $da['state']=input('post.state');
        $Yan_zheng = new Yan_zheng();
        if(($error = $Yan_zheng ->is_string($da)) != 1){
            return $error;
        }
        $state =  Db::table('q_money')->update($da);
        $Yan_zheng ->rizhi("修改拨款状态,状态：{$state},内容：".implode('=',$da));
        return $state;
    }
    //添加拨出金额
    public function add_info($id)
    {
        $Yan_zheng = new Yan_zheng();
        if(\request() ->isAjax()){

            $da['panen_id'] = input('post.parend_id');
            $da['money'] = input('post.money');
            $da['money_img'] = input('post.money_img');
            if(($error = $Yan_zheng ->is_string($da)) != 1){
                return $error;
            }
            return Db::table('q_money')->insert($da);
        }
        if(($error = $Yan_zheng ->is_number([$id])) != 1){
            return $error;
        }
        return view();
    }
    //完善土建资料
    public function rmb_info($id)
    {
        $Yan_zheng = new Yan_zheng();


        //完善项目数据
        if (\request() ->isAjax()) {
            $da['id'] = input('post.id');
            $da['user_name'] = input('post.user_name');
            $da['mobile'] = input('post.mobile');
            $da['user_representative'] = input('post.user_representative');
            $da['user_representative_mobile'] = input('post.user_representative_mobile');
            $da['manager'] = input('post.manager');
            $da['certificates'] = input('post.certificates');
            $da['certificates_mobile1'] = input('post.certificates_mobile1');
            $da['certificates_mobile2'] = input('post.certificates_mobile2');
            $da['budgetary_price'] = input('post.budgetary_price');
            $da['determine_mobile'] = input('post.determine_mobile');
            $da['budgetary_price_img'] = input('post.budgetary_price_img');
            $da['determine_mobile_img'] = input('post.determine_mobile_img');
            if(($error = $Yan_zheng ->is_string($da)) != 1){
                return $error;
            }
            $state =  Db::table('q_datalist')->update($da);
            $Yan_zheng ->rizhi("完善或修改一个项目资金,状态：{$state},内容：".implode('=',$da));
            return $state;
        }
        if(($error = $Yan_zheng ->is_number([$id])) != 1){
            return $error;
        }
        $vi['data'] = Db::table('q_datalist')->find($id);
        return view('',$vi);
    }
    //资金使用页面
    public function rmb($id)
    {
        if(\request()->isAjax()){
            return Db::table('q_money')->where('panen_id',$id)->select();
        }
        $vi['data'] = Db::table('q_datalist')->find($id);
        return view('',$vi);
    }
    //竣工手续
    public function be_formalities($id)
    {
        $Yan_zheng = new Yan_zheng();
        if(($error = $Yan_zheng ->is_number([$id])) != 1){
            return $error;
        }
        if(\request()->isAjax()){
            return Db::table('q_start')
                ->where('parend_id',$id)
                ->where('type',1)
                ->select();
        }

        return view('');
    }
    //拒绝通过手续
    public function procedures_up()
    {
        $da['state']=input('post.state');

        $Yan_zheng = new Yan_zheng();
        if(($error = $Yan_zheng ->is_number([$da['state'],input('post.id')])) != 1){
        return $error;
    }
        if($da['state'] < 0 || $da['state'] >3){
            $da['state'] = 3;
        }
        $state =  Db::table('q_start')
            //->where('state',3)
            ->where('id',input('post.id'))
            ->update($da);
        $Yan_zheng ->rizhi("修改一个手续状态，状态是{$state},内容是：".implode('=',$da));
        return $state;
    }
    //办理手续
    public function procedures($id)
    {
        $Yan_zheng = new Yan_zheng();
        if (($error = $Yan_zheng->is_string([$id])) != 1) {
            return $error;
        }
        //修改数据
        if(\request() ->isAjax()){
            $da['id'] = input('post.id');
            $da['index'] = input('post.index');
            $da['company'] = input('post.company');
            $da['company_img'] = input('post.company_img');
            $da['money'] = input('post.money');
            #$da['payment'] = input('post.payment');
            $da['payment_money'] = input('post.payment_money');
            $da['payment_img'] = input('post.payment_img');
            $da['text'] = input('post.text');
            $da['state'] = 3;
            if (($error = $Yan_zheng->is_string($da)) != 1) {
                return $error;
            }
            $state =  Db::table('q_start')->update($da);
            $Yan_zheng ->rizhi("修改手续内容，状态：{$state},内容：".implode('=',$da));
            return $state;
        }
        $vi['data'] = Db::table('q_start')
            ->where('id',$id)
            ->find();
        return view('',$vi);
    }
    //添加手续名称
    public function procedures_name()
    {
        $da['parend_id'] = input('post.id');
        $da['name'] = input('post.name');
        $da['type'] = input('post.type');
        $Yan_zheng = new Yan_zheng();
        if (($error = $Yan_zheng->is_string($da)) != 1) {
            return $error;
        }
        $state =  Db::table('q_start')->insert($da);
        $Yan_zheng->rizhi("添加 一个手续名称,状态：{$state}，内容：".implode('=',$da));
        return $state;
    }
    //开工手续
    public function commencement_formalities($id)
    {
        $type =  input('type') !=1?0:1;
        $Yan_zheng = new Yan_zheng();
        if(($error = $Yan_zheng ->is_number([$id])) != 1){
            return $error;
        }
        if(\request()->isAjax()){
            return Db::table('q_start')
                ->alias('sta')
                ->where('sta.parend_id',$id)
                ->where('sta.type',$type)
                ->join('q_appropriation app','sta.id = app.parent_id','left')
                ->group('sta.id')
               ->field("
                sum(app.money) as appmoney,
                sta.id as staid,/*主键*/
                sta.name as staname,/*名称*/
                sta.index as staindex,/*办理手续文号*/
                sta.company as stacompany,/*开具证明单位*/
                sta.company_img as stacompany_img,/*单位证据*/
                sta.text as statext,/*备注*/
                sta.state as stastate,/*状态*/
                sta.error as staerror,/*拒绝理由*/
                sta.money as stamoney/*办理费用*/
               ")
                ->select();
        }

        return view('');
    }
    //设备进度
    public function complete($id)
    {
        $Yan_zheng = new Yan_zheng();
        if(($error = $Yan_zheng ->is_number([$id])) != 1){
            return $error;
        }
        if(\request() ->isAjax()){
            return Db::table('q_speed')->where('parend_id',$id)->select();
        }
        return view();
    }
    //添加进度
    public function addd($id)
    {
        $Yan_zheng = new Yan_zheng();
        if(($error = $Yan_zheng ->is_number([$id])) != 1){
            return $error;
        }
        //添加内容
        if(\request() ->isAjax()){
            $da['parend_id'] = input('post.parend_id');
            $da['text'] = input('post.text');
            $da['img'] = input('post.img');
            if(($error = $Yan_zheng ->is_string($da)) != 1){
                return $error;
            }
            $state = Db::table('q_speed')->insert($da);
            $Yan_zheng ->rizhi("添加一个进度，状态：{$state}，内容".implode('=',$da));
            return $state;

        }
       return view();
    }
    //土建工程进度
    public function speed($id)
    {
        $Yan_zheng = new Yan_zheng();
        if(($error = $Yan_zheng ->is_number([$id])) != 1){
            return $error;
        }
        if(\request() ->isAjax()){
            return Db::table('q_speed')->where('parend_id',$id)->select();
        }
        return view();
    }
    //添加土建
    public function index()
    {
        //接收数据
        if(\request() ->isAjax()){
            $Yan_zheng = new Yan_zheng();
            $da['category']=input('post.category');
            $da['school']=input('post.school');
            $da['name']=input('post.name');
            $da['type']=input('post.type');
            $da['content']=input('post.content');
            $da['count']=input('post.count');
            $da['money']=input('post.money');
            $da['address1']=input('post.address1');
            $da['address2']=input('post.address2');
            $da['address3']=input('post.address3');
            $da['address4']=input('post.address4');
            $da['date']=input('post.date');
            $da['money_img']=input('post.money_img');
            //验证
            if(($error = $Yan_zheng ->is_string($da)) != 1){
                return $error;
            }
            $state = Db::table('q_datalist')->insert($da);

            $Yan_zheng ->rizhi("添加一个土建,状态是{$state}，内容是：".implode('=',$da));
            return $state;

        }
        return view();
    }

    //添加设备购置页面
    public function equipment()
    {
        if(\request() ->isAjax()){
            //获取名字对应的id
            $Yan_zheng = new Yan_zheng();
            if(($error = $Yan_zheng ->is_string([input('post.name')])) != 1){
                return $error;
            }
            $id = Db::table('q_datalist')
                ->where('state',1)
                ->where('name',input('post.name'))
                ->value('id');
            //验证数字
            if(($error = $Yan_zheng ->is_number([$id])) != 1){
                return $error;
            }
            $state = Db::table('q_equipment')->insert(['datalist_id'=>$id]);
            $Yan_zheng ->rizhi("添加一个购置设备,状态：{$state}，工程id是：{$id}");
            return $state;
            //添加日志
        }
        $vi['name'] = Db::table('q_datalist')
            ->field("
            name as label
            ")
            ->where('state',1)
            ->select();
        $vi['name'] = json_encode($vi['name']);
        return view('',$vi);
    }
    
    //验证项目名称是否存在 0:存在返回失败，1：成功返回成功
    public function is_name()
    {
       $count =  Db::table('q_datalist')->where('name',input('param'))->count();
        if($count == $_GET['type']){
            return ['info'=>[],'status'=>'y'];
        }
        else{
            return ['info'=>[],'status'=>'n'];
        }

    }

    //土建数据通过拒绝删
    public function uptyle()
    {
        $Yan_zheng = new Yan_zheng();
        if (($error = $Yan_zheng ->is_number([$_POST['id'],$_POST['type']])) != 1) {
            return $error;
        }

        $_POST['type'] =$_POST['type'] > 3 ? 2:$_POST['type'];//限制2以内
        if($_POST['type'] == 3){
            $state = Db::table('q_datalist')->delete($_POST['id']);
            $Yan_zheng ->rizhi("删除土建数据,删除状态是：{$state},id:{$_POST['id']}");
            return $state;
        }

        $state = Db::table('q_datalist')
            //->where('state',0)
            ->where('id',$_POST['id'])
            ->update(['state'=>$_POST['type']]);
        $Yan_zheng ->rizhi("修改一个土建状态,修改状态是：{$state},修改成{$_POST['type']}，（0：待审核 1：通过 2：拒绝）");
        return $state;

    }

    //设备通过和删除
    public function upshebei()
    {
        $Yan_zheng = new Yan_zheng();
        if (($error = $Yan_zheng ->is_number([$_POST['id'],$_POST['type']])) != 1) {
            return $error;
        }

        $_POST['type'] =$_POST['type'] != 1 && $_POST['type'] != 3 ? 0:$_POST['type'];//限制1,3以内
        if($_POST['type'] == 3){
            $state = Db::table('q_equipment')->delete($_POST['id']);
            $Yan_zheng ->rizhi("删除设备数据,删除状态是：{$state},id:{$_POST['id']}");
            return $state;
        }

        $state = Db::table('q_equipment')
            ->where('state',0)
            ->where('id',$_POST['id'])
            ->update(['state'=>$_POST['type']]);
        $Yan_zheng ->rizhi("修改一个设备状态,修改状态是：{$state},修改成{$_POST['type']}，（0：待审核 1：通过 2：拒绝）");
        return $state;
    }

    //修改土建数据
    public function upindex($id)
    {
        //获取数据
        $Yan_zheng = new Yan_zheng();
        //判断是不是接收数据
        if(\request() ->isAjax()){
            $Yan_zheng = new Yan_zheng();
            $da['id']=input('post.id');
            $da['school']=input('post.school');
            $da['type']=input('post.type');
            //$da['name']=input('post.name');
            $da['content']=input('post.content');
            $da['count']=input('post.count');
            $da['money']=input('post.money');
            $da['money_img']=input('post.money_img');
            $da['address1']=input('post.address1');
            $da['address2']=input('post.address2');
            $da['address3']=input('post.address3');
            $da['address4']=input('post.address4');
            $da['date']=input('post.date');
            $da['state']=0;
            //验证
            if(($error = $Yan_zheng ->is_string($da)) != 1){
                return $error;
            }
            $state = Db::table('q_datalist')->update($da);

            $Yan_zheng ->rizhi("修改一个土建,状态是{$state}，内容是：".implode('=',$da));
            return $state;
        }
        //验证id
        if(($error = $Yan_zheng ->is_number([$id])) != 1){
            return $Yan_zheng ->is_number([$id]);
        }
        $vi['data'] = Db::table('q_datalist')->find($id);
        //验证数据
        if (is_null($vi['data'])) {
            exit('404');
        }
        return view('',$vi);
    }

}
